package gwu.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CreateArticle extends Activity{
	private EditText mArticleID;
	private int articleID;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_article);
		setTitle("Add Article");

		Button confirmButton = (Button) findViewById(R.id.confirm);
		confirmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				new Thread(new Runnable() {
					public void run() {
						SendHttpRequest();
					}
				}).start();
			}
		});

	}
	private void SendHttpRequest(){
		Bundle bundle = new Bundle();

		//read the article ID from the input text box
		mArticleID = (EditText) findViewById(R.id.articleID);

		//generate the HTTP request string
		//change the IP to your local computer
		String url = "http://128.164.204.67:8080/ArticleRequestServlet/ArticleRequest?articleID=";
		url = url + mArticleID.getText();
		//url.concat(mArticleID.getText().toString());
		System.out.println(url);

		//make the http request to the application server
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		HttpResponse response;

		JSONObject json = new JSONObject();

		try {
			response = httpclient.execute(httpget);

			HttpEntity entity = response.getEntity();

			if (entity != null) {
				// A Simple JSON Response Read
				InputStream instream = entity.getContent();
				String result= convertStreamToString(instream);

				//create a Json Object with the result string
				json = new JSONObject(result);
				//int lexile = json.getInt("lexile");
				String articleTitle = json.getString("title");
				String articleBody = json.getString("body");
				JSONArray arrayItems = json.getJSONArray("items");
				ArrayList<WordClozeItem> items = new ArrayList<WordClozeItem>() ;

				ArrayList<Integer> wordIDs = new ArrayList<Integer>();
				ArrayList<String> answers = new ArrayList<String>();
				ArrayList<String> foils1 = new ArrayList<String>();
				ArrayList<String> foils2 = new ArrayList<String>();
				ArrayList<String> foils3 = new ArrayList<String>();
				for(int i = 0; i <arrayItems.length() ; i++)
				{
					JSONObject item = new JSONObject(arrayItems.get(i).toString());
					int index = item.getInt("index");
					String correct = item.getString("correct");
					String option1 = item.getString("option1");
					String option2 = item.getString("option2");
					String option3 = item.getString("option3");
					WordClozeItem word = new WordClozeItem(index, correct, option1, option2, option3, 0);
					items.add(word);
					wordIDs.add(index);
					answers.add(correct);
					foils1.add(option1);
					foils2.add(option2);
					foils3.add(option3);
				}
				bundle.putString(ArticleDbAdapter.KEY_TITLE, articleTitle);
				bundle.putString(ArticleDbAdapter.KEY_BODY, articleBody);
				bundle.putIntegerArrayList(WordItemsDbAdapter.KEY_WORDID, wordIDs);
				bundle.putStringArrayList(WordItemsDbAdapter.KEY_ANSWER, answers);
				bundle.putStringArrayList(WordItemsDbAdapter.KEY_FOIL1, foils1);
				bundle.putStringArrayList(WordItemsDbAdapter.KEY_FOIL2, foils2);
				bundle.putStringArrayList(WordItemsDbAdapter.KEY_FOIL3, foils3);

				instream.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (JSONException e) {
			e.printStackTrace();
		}



		Intent mIntent = new Intent();
		mIntent.putExtras(bundle);
		setResult(RESULT_OK, mIntent);
		finish();
	}

	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}

//            
//            public void onClick(View view) {
//                Bundle bundle = new Bundle();
//                mArticleID = (EditText) findViewById(R.id.articleID);
//                articleID = Integer.parseInt(mArticleID.getText().toString());
//                
//                Article article = ArticleManager.getInstance().getArticle(articleID);
////                ArrayList<Object_WordClozeItem> items = 
////                	WordClozeGenerator.getInstance().clozeText(article.getArticleText(), article.getLexile());
//                
//                ArrayList<Integer> wordIDs = new ArrayList<Integer>();
//                ArrayList<String> answers = new ArrayList<String>();
//                ArrayList<String> foils1 = new ArrayList<String>();
//                ArrayList<String> foils2 = new ArrayList<String>();
//                ArrayList<String> foils3 = new ArrayList<String>();
//
//                wordIDs.add(61);
//                answers.add("sports");
//                foils1.add("countries");
//                foils2.add("classes");
//                foils3.add("things");
//                
//                bundle.putString(ArticleDbAdapter.KEY_TITLE, article.getTitle());
//                bundle.putString(ArticleDbAdapter.KEY_BODY, article.getArticleText());
//                bundle.putIntegerArrayList(WordItemsDbAdapter.KEY_WORDID, wordIDs);
//                bundle.putStringArrayList(WordItemsDbAdapter.KEY_ANSWER, answers);
//                bundle.putStringArrayList(WordItemsDbAdapter.KEY_FOIL1, foils1);
//                bundle.putStringArrayList(WordItemsDbAdapter.KEY_FOIL2, foils2);
//                bundle.putStringArrayList(WordItemsDbAdapter.KEY_FOIL3, foils3);
//
//
//                Intent mIntent = new Intent();
//                mIntent.putExtras(bundle);
//                setResult(RESULT_OK, mIntent);
//                finish();
//            }
//        });

